package com.gr100.biz.app.res;

import cn.dreampie.route.core.annotation.API;
import cn.dreampie.route.core.annotation.GET;
import cn.dreampie.route.core.annotation.POST;
import com.gr100.biz.api.order.OrderService;
import com.jfinal.log.Log;
import com.mysql.jdbc.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * Created by YFsoft on 16/3/14.
 */

@API("/order")
public class OrderResource extends ApiResource {

    private static final Log logger = Log.getLog(OrderResource.class);

    OrderService orderService = new OrderService();

    @GET("/create")
    public String create(String type,String toProvince,String toCity,String toName,String toPhone,String toStreet,String total,String finalAmount,String discount,int gCoinChecked,String gCoin,String remark,String areaName,String shopName,String uid,String couponId,String par,String bookdate,String expresscost,String expressname,String expresstype,String deliveryTime,int UserTimingCouponId,int MemberTimingCouponId,String TimingPrice,String mj_title,String mj_amount,String couponDiscountId,String couponDiscountPar) {

        try {
            //特殊处理 中文转码
            if(!StringUtils.isNullOrEmpty(toProvince)){
                toProvince = new String(toProvince.getBytes("ISO-8859-1"));
            }else{
                toProvince = "江苏省";
            }
            if(!StringUtils.isNullOrEmpty(toCity)){
                toCity = new String(toCity.getBytes("ISO-8859-1"));
            }else{
                toCity = "扬州市";
            }
            if(!StringUtils.isNullOrEmpty(toName)){
                toName = new String(toName.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(toPhone)){
                toPhone = new String(toPhone.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(toStreet)){
                toStreet = new String(toStreet.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(remark)){
                remark = new String(remark.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(areaName)){
                areaName = new String(areaName.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(shopName)){
                shopName = new String(shopName.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(expressname)){
                expressname = new String(expressname.getBytes("ISO-8859-1"));
            }
            if(!StringUtils.isNullOrEmpty(expresstype)){
                expresstype = new String(expresstype.getBytes("ISO-8859-1"));
            }
            if(StringUtils.isNullOrEmpty(deliveryTime)){
                deliveryTime = "0";
            }
            if(!StringUtils.isNullOrEmpty(mj_title)){
                mj_title = new String(mj_title.getBytes("ISO-8859-1"));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Map map = orderService.createOrder(type,toProvince,toCity,toName,toPhone,toStreet,total,finalAmount,discount,gCoinChecked,gCoin,remark,areaName,shopName,uid,couponId,par,bookdate,expresscost,expressname,expresstype,deliveryTime,UserTimingCouponId,MemberTimingCouponId,TimingPrice,mj_title,mj_amount,couponDiscountId,couponDiscountPar);
        return echo(map);
    }

    @GET("/pay")
    public String pay(String orderid,double amount,String payway) {

        try {
            //特殊处理 中文转码
            if(!StringUtils.isNullOrEmpty(payway)){
                payway = new String(payway.getBytes("ISO-8859-1"));
            }else{
                payway = "货到付款";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Map map = orderService.pay(orderid, amount, payway);
        return echo(map);
    }

    @GET("/notify")
    public String notify(String orderid,double amount,String payway) {

        try {
            //特殊处理 中文转码
            if(!StringUtils.isNullOrEmpty(payway)){
                payway = new String(payway.getBytes("ISO-8859-1"));
            }else{
                payway = "货到付款";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        Map map = orderService.notify(orderid, amount, payway);
        return echo(map);
    }

    @GET("/recharge")
    public String recharge(String uid,double amount) {

        Map map = orderService.recharge(uid, amount);
        return echo(map);
    }

    @GET("/rechargenotify")
    public String rechargenotify(String orderid,double amount) {

        Map map = orderService.rechargenotify(orderid, amount);
        return echo(map);
    }
}
